// $Id: uc_ajax_cart.js,v 1.1.2.11 2010/05/01 12:42:30 erikseifert Exp $

Drupal.behaviors.ucAjaxCart = function (context) {

  if (!Drupal.uc_ajax_cart) {

    // First initialization.

    // Set up UC Ajax Cart namespace.
    Drupal.uc_ajax_cart = {};

    // Populate namespace.
    Drupal.uc_ajax_cart.cart_open_state = true;
    Drupal.uc_ajax_cart.cart_wrapper = jQuery('#block-uc_ajax_cart-0', context);
    Drupal.uc_ajax_cart.cart_pane = jQuery('#ajaxCartUpdate #cart-block-contents-ajax', context);
    Drupal.uc_ajax_cart.unblock_handler = function () { blocked = false;}
    Drupal.uc_ajax_cart.blockUI_blocked = true;
    

    // BlockUI settings.
    jQuery.blockUI.defaults.growlCSS.opacity = 1;
    jQuery.blockUI.defaults.timeout = Drupal.settings.uc_ajax_cart.TIMEOUT;    
    jQuery.blockUI.defaults.onUnblock = Drupal.uc_ajax_cart.unblock_handler;


    // Other one time processes.
    ///////////////////////////

    // Add open cart class because initially is opened.
    Drupal.uc_ajax_cart.cart_wrapper.addClass('cart-open');

    // Check open state tracking.
    if (Drupal.settings.uc_ajax_cart.TRACK_CLOSED_STATE) {      
      ajaxCartCheckCookieCartState();
    }
    else if (Drupal.settings.uc_ajax_cart.INITIAL_CLOSED_STATE) {
      // Close cart block.
      ajaxCartCloseCart();
    }
    else {
      // Add open cart class.
      Drupal.uc_ajax_cart.cart_wrapper.addClass('cart-open');
    }
  }


  // Set up ajax-cart-view-handler.
  $('#ajax-cart-view-handler', context).attr('href', '#')
  .text(Drupal.t('Load cart content'))
  .click(ajaxCartUpdateBlockCart);



  // Ubercart Cart links support.
  jQuery('a.ajax-cart-link', context).not('.ajax-cart-processed').bind('click', function () {
    ajaxCartBlockUI(Drupal.settings.uc_ajax_cart.ADD_TITLE,
                    '<div class="messages status">' + Drupal.settings.uc_ajax_cart.ADD_MESSAGE + '</div>');
    jQuery.get(Drupal.settings.uc_ajax_cart.CART_LINK_CALLBACK,
               { href: this.href },
               ajaxCartFormSubmitted);

    return false;
  })
  .addClass('ajax-cart-processed');

  // Ubercart attributes related.
  jQuery('form.ajax-cart-submit-form input.ajax-cart-submit-form-button', context).not('.ajax-cart-processed, #edit-update').click(function () {
    var form = jQuery(this).parents('form').eq(0);
    form.ajaxSubmit({
      url : Drupal.settings.uc_ajax_cart.CALLBACK,
      beforeSubmit : function () {
        ajaxCartBlockUI(Drupal.settings.uc_ajax_cart.ADD_TITLE,
                        '<div class="messages status">' + Drupal.settings.uc_ajax_cart.ADD_MESSAGE + '</div>')},
      success : ajaxCartFormSubmitted,
      type : 'post'
    });
    return false;
  })
  .addClass('ajax-cart-processed');

  // Check for autoupdate cart block.
  if (jQuery('#ajaxCartUpdate', context).not('.ajax-cart-processed').hasClass('load-on-view')) {    
    jQuery('#ajaxCartUpdate').addClass('ajax-cart-processed');
    ajaxCartUpdateBlockCart();
  }


  // Call behaviors over cart block.
  ajaxCartCartBlockBehaviors(context);

  // Call behaviors over cart page.
  ajaxCartCartPageBehaviors(context);

}


// Process behaviors for the cart from cart page.
function ajaxCartCartPageBehaviors(context) {
  // Set handler for cart submit button.
  jQuery('#uc-cart-view-form #edit-update', context).not('.ajax-cart-processed').bind('click', function () {
    jQuery(this).parents('form').ajaxSubmit({
        url: Drupal.settings.uc_ajax_cart.UPDATE_CALLBACK,
        success: ajaxCartUpdateCartView,
        beforeSubmit: function () {
          jQuery('#uc-cart-view-form input').attr('disabled', 'disabled');
          ajaxCartBlockUI(Drupal.settings.uc_ajax_cart.ADD_TITLE, '<div class="messages status">' + Drupal.settings.uc_ajax_cart.UPDATE_MESSAGE + '</div>');
        }
    });
    return false;
  })
  .addClass('ajax-cart-processed');

  // Trigger submit button when cart form input elements are changed.
  jQuery('#uc-cart-view-form input', context).not('.ajax-cart-processed').bind('change', function () {
    jQuery('#uc-cart-view-form #edit-update').trigger('click');
  })
  .addClass('ajax-cart-processed');
}


// Process behaviors for the cart block.
function ajaxCartCartBlockBehaviors(context) {  
  Drupal.uc_ajax_cart.cart_pane = jQuery('#ajaxCartUpdate #cart-block-contents-ajax', context);

  $('#ajaxCartToggleView', context).not('.ajax-cart-processed').click(function () {
    ajaxCartToggleView();
    return false;
  })
  .addClass('ajax-cart-processed');
}


// Opens cart block.
// Sets cookie if track open state enabled.
function ajaxCartOpenCart() {  
  if (!Drupal.uc_ajax_cart.cart_open_state) {
    Drupal.uc_ajax_cart.cart_open_state = true;
    if (Drupal.settings.uc_ajax_cart.CART_PANE_EFFECT) {
      Drupal.uc_ajax_cart.cart_pane.slideDown(Drupal.settings.uc_ajax_cart.CART_PANE_EFFECT_DURATION);
    }
    else {
      Drupal.uc_ajax_cart.cart_pane.show();
    }
    Drupal.uc_ajax_cart.cart_wrapper.addClass('cart-open');

    if (Drupal.settings.uc_ajax_cart.TRACK_CLOSED_STATE) {      
      jQuery.cookie('ajax-cart-visible', '1', { path: '/'});
    }
  }
}


// Closes cart block.
// Sets cookie if track open state enabled.
function ajaxCartCloseCart() {  
  if (Drupal.uc_ajax_cart.cart_open_state) {
    Drupal.uc_ajax_cart.cart_open_state = false;
    if (Drupal.settings.uc_ajax_cart.CART_PANE_EFFECT) {
      Drupal.uc_ajax_cart.cart_pane.slideUp(Drupal.settings.uc_ajax_cart.CART_PANE_EFFECT_DURATION);
    }
    else {
      Drupal.uc_ajax_cart.cart_pane.hide();
    }
    Drupal.uc_ajax_cart.cart_wrapper.removeClass('cart-open');

    if (Drupal.settings.uc_ajax_cart.TRACK_CLOSED_STATE && (jQuery.cookie('ajax-cart-visible', { path: '/'}) != '0')) {      
      jQuery.cookie('ajax-cart-visible', '0', { path: '/'});
    }
  }
}


// Initialize cart page ajax update feature.
// Simply call behaviors for cart page with right context.
function ajaxCartInitCartView() {
  ajaxCartCartPageBehaviors($('#cart-form-pane'));
}

// Initialize cart block.
// Simply call behaviors for cart block with right context.
function ajaxCartInitCartBlock() {
  ajaxCartCartBlockBehaviors($('#ajaxCartUpdate').parent());
  // Cart has been loaded, so it's shown, cange state accordingly.
  Drupal.uc_ajax_cart.cart_open_state = true;
  ajaxCartCheckCookieCartState();
}


// Checks open state cookie and changes cart open state accordingly.
function ajaxCartCheckCookieCartState() {  
  var cookie_state = jQuery.cookie('ajax-cart-visible'); 

  if (Drupal.uc_ajax_cart.cart_open_state != cookie_state) {
    if (cookie_state == true) {
      ajaxCartOpenCart();
    }
    else {
      ajaxCartCloseCart();
    }
  }
}


// Show message using BlockUI anc configured layout.
function ajaxCartShowMessageProxy(title, message) {
  // Check if UI is blocked. Blocked UI implies no fader in for no flickering.
  var fadein = 0;
  if (!Drupal.uc_ajax_cart.blockUI_blocked) {
    Drupal.uc_ajax_cart.blockUI_blocked = true;
    fadein = 500;
  }

  if (Drupal.settings.uc_ajax_cart.BLOCK_UI == 1) {
    jQuery.blockUI({ message : '<h2>' + title + '</h2>' + message, fadeIn: fadein });
  }
  else {    
    var $m = $('<div class="growlUI"></div>');
    if (title) {
      $m.append('<h1>' + title + '</h1>');
    }

    if (message) {
      $m.append('<h2>' + message + '</h2>');
    }

    jQuery.blockUI({
      message: $m,
      fadeIn: fadein,
      fadeOut: 700,
      showOverlay: false,
      centerY: false,
      css: {
        width: '350px',
        top: '10px',
        left: '',
        right: '10px',
        border: 'none',
        padding: '5px',
        backgroundColor: '#000',
        '-webkit-border-radius': '10px',
        '-moz-border-radius': '10px',
        color: '#fff',
        opacity: 1
      }
    });
  }  
}


function ajaxCartShowMessageProxyClose() {
  jQuery.unblockUI();
}


// Toggle cart block.
function ajaxCartToggleView() {
  Drupal.uc_ajax_cart.cart_open_state ? ajaxCartCloseCart() : ajaxCartOpenCart();
}


// Processes after cart form is submitted.
function ajaxCartFormSubmitted(e) {
  // Enable input elements from cart from cart page.
  jQuery('form.ajax-cart-submit-form input').attr('disabled', false);

  // Update cart block.
  ajaxCartUpdateBlockCart();


  ajaxCartBlockUI(Drupal.settings.uc_ajax_cart.CART_OPERATION, e);
  ajaxCartReloadCartView();
}


function ajaxCartBlockUI(title, message) {
  ajaxCartShowMessageProxy(title, message);
}


function ajaxCartBlockUIRemove(url) {
  jQuery('#uc-cart-view-form input').attr('disabled', 'disabled');
  ajaxCartShowMessageProxy(Drupal.settings.uc_ajax_cart.REMOVE_TITLE, Drupal.settings.uc_ajax_cart.REMOVE_MESSAGE);
  jQuery.post(url, ajaxCartFormSubmitted);
  return false;
}


// Loads cart block contents using ajax.
function ajaxCartUpdateBlockCart() {
  jQuery('#ajaxCartUpdate').load(Drupal.settings.uc_ajax_cart.SHOW_CALLBACK, {}, ajaxCartInitCartBlock);

  return false;
}


// Updates cart from cart page.
function ajaxCartUpdateCartView(e) {
  ajaxCartFormSubmitted(e);
  ajaxCartReloadCartView();
}


function ajaxCartReloadCartView() {
  jQuery('#cart-form-pane').parent().load(Drupal.settings.uc_ajax_cart.SHOW_VIEW_CALLBACK, ajaxCartInitCartView);
}


function ajaxCartUpdateCartViewUpdated(e) {
  ajaxCartUpdateBlockCart();
  ajaxCartInitCartView();
}


function ajaxCartShowMessages(e) {
  if (e != "") {
    clearTimeout();
    ajaxCartShowMessageProxy('Message', e);
  }
}

